﻿// ==UserScript==
// @name         chrome-new-tab
// @namespace    https://user-web.googlecode.com/
// @version      0.1
// @description  insert bookmarks into chrome's new tab
// @match        https://www.google.co.uk/_/chrome/newtab*
// @copyright    2010+, james_zerty
// @require      https://user-web.googlecode.com/svn/trunk/Scripts/jquery/jquery-2.1.0.js
// ==/UserScript==
var isTop = true;

try {
    isTop = (parent && parent.location && parent.location.href == document.location.href);
}
catch (e) {
    console.log("[new-tab] Error checking for top frame: " + e.message);
    isTop = false;
}

if (isTop) {
    var newTab = new function () {
        var me = this;
        var url = document.location.href;
        var $style = null;

        me.IsLinux = navigator.appVersion.indexOf("Linux") != -1;

        me.Debug = {
            LocalUrls: 1,
            UniqueUrls: 1,
            Reloader: 1
        };

        if (me.Debug.Reloader) {
            $(document).dblclick(function () {
                document.location.href = document.location.href;
            });
        }

        me.GetRoot = function () {
            return me.Debug.LocalUrls ? "https://localhost:44300/" : "https://new-tab.googlecode.com/svn/trunk/";
        }

        me.Load = function () {
            me.Log("url", url);
        };

        me.LoadNewTab = function () {
            me.LoadStyle("chrome-new-tab");

            $("#mngb, #f, #lga, #mv-tiles, .mv-row, #mv-noti, #prm-pt").remove();

            var p = $("#most-visited");

            /*
            var t = me.CreateElement(p, "table");
            var tr1 = me.CreateElement(t, "tr");
            var td1 = me.CreateElement(tr1, "td");
            var td2 = me.CreateElement(tr1, "td");
            var td3 = me.CreateElement(tr1, "td");
            var td4 = me.CreateElement(tr1, "td");
            var td5 = me.CreateElement(tr1, "td");
            var td6 = me.CreateElement(tr1, "td");

            me.CreateLink(td1, "Feedly", "feedly1", "https://feedly.com/index.html#index");
            me.CreateLink(td2, "Pocket", "pocket", "https://getpocket.com/a/queue/list/");
            me.CreateLink(td3, "Outlook", "outlook", urlOutlook);
            me.CreateLink(td4);
            me.CreateLink(td5, "News", "bbc-news", "http://www.bbc.co.uk/news/");
            me.CreateLink(td6, "Router", "router", urlRouter);

            me.CreateLink(td1, "Gmail", "gmail", "https://mail.google.com/mail/u/0/#inbox");
            me.CreateLink(td2, "Calendar", "google-calendar", "https://www.google.com/calendar/render?tab=mc");
            me.CreateLink(td3, "Tasks", "google-tasks", "https://mail.google.com/tasks/canvas?pli=1");
            me.CreateLink(td4);
            me.CreateLink(td5, "Weather", "bbc-weather", "http://www.bbc.co.uk/weather/2657402");
            me.CreateLink(td6, "Amazon", "amazon", "https://www.amazon.co.uk/gp/css/homepage.html/ref=topnav_ya");

            me.CreateLink(td1);
            me.CreateLink(td2);
            me.CreateLink(td3);
            me.CreateLink(td4);
            me.CreateLink(td5);
            me.CreateLink(td6);

            me.CreateLink(td1, "Google", "google", "https://www.google.co.uk/");
            me.CreateLink(td2, "Contacts", "google-contacts", "https://www.google.com/contacts/u/0/#contacts");
            me.CreateLink(td3, "Maps", "google-maps", urlMaps);
            me.CreateLink(td4, "Drive", "google-drive", "https://drive.google.com/#my-drive");
            me.CreateLink(td5, "Play", "google-play", "https://play.google.com/store");
            me.CreateLink(td6, "Google+", "google-plus1", "https://plus.google.com/u/0/photos/yourphotos");
            */
        };

        me.CreateLink = function (parent, text, image, link) {
            var link = me
                .CreateElement(parent, "a")
                .attr("href", link);

            if (image == null) {
                return;
            }

            var img = me
                .CreateElement(link, "img")
                .attr("src", me.GetRoot() + "Styles/buttons/" + image + ".png?123");

            var text = me
                .CreateElement(link, "span")
                .html(text);
        };

        me.CreateElement = function (parent, tag, className, text) {
            var el = $("<" + tag + "></" + tag + ">");

            if (className != null) {
                el.addClass(className);
            }

            if (text != null) {
                el.text(text);
            }

            if (parent != null) {
                $(parent).append(el);
            }

            return el;
        };

        me.LoadStyle = function (fileName) {
            var uniqueUrl = me.Debug.UniqueUrls ? '?' + new Date().valueOf() : '';
            var src = me.GetRoot() + 'Styles/' + fileName + '.css' + uniqueUrl;
            var s = document.createElement('link');
            s.setAttribute('id', fileName + "-style");
            s.setAttribute('href', src);
            s.setAttribute('rel', 'stylesheet');
            s.setAttribute('type', 'text/css');
            s.setAttribute('data-local', false);
            document.head.appendChild(s);
        };

        me.Log = function (text1, text2) {
            if (text2 != null) {
                if (text1.length < 20) {
                    text1 = text1 + Array(21 - text1.length).join(" ");
                }

                console.log("[new-tab] " + text1 + ": " + text2);
            }
            else {
                console.log("[new-tab] " + text1);
            }
        };
    };

    if (document.location.href.match(/google.*chrome.*newtab/i) != null) {
        newTab.Load();
        newTab.LoadNewTab();
    }
}